package com.example.hyznbk.view.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

import com.example.hyznbk.R;
import com.example.hyznbk.view.base.BaseActivity;
import com.example.hyznbk.view.fragment.WrongReviewFragment;
import com.example.hyznbk.view.widget.RevealBackgroundView;

/**
 * 基本复习中，错题的基本复习
 * Created by tangminglong on 17/8/10.
 */

public class WrongReviewActivity extends BaseActivity implements RevealBackgroundView.OnStateChangeListener{

    public static final String WRONG_FROM = "WrongFrom";
    public static final String ARG_REVEAL_START_LOCATION = "reveal_start_location";
    Bundle savedInstanceState;
    private RevealBackgroundView mRevealBackgroundView;
    private FrameLayout mFrameLayout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setBackgroundDrawable(null);
        this.savedInstanceState  = savedInstanceState;
        super.onCreate(savedInstanceState);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void updateStatusBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(0xff111111);
        }
    }

    public static void launch(Context mContext, int from, int[] startingLocation){
        Intent intent = new Intent(mContext, WrongReviewActivity.class);
        intent.putExtra(WRONG_FROM,from);
        intent.putExtra(ARG_REVEAL_START_LOCATION,startingLocation);
        mContext.startActivity(intent);
        ((Activity)mContext).overridePendingTransition(0,0);

    }

    @Override
    public int attachLayoutRes() {
        return R.layout.activity_wrong_review;
    }

    @Override
    protected void initViews() {
        mRevealBackgroundView = (RevealBackgroundView) findViewById(R.id.vRevealBackground);
        mFrameLayout = (FrameLayout) findViewById(R.id.liner_container);
        //updateStatusBarColor();

        setupRevealBackground();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.liner_container,new WrongReviewFragment());
        transaction.commit();


    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }



    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit);
    }


    public void setupRevealBackground() {
        mRevealBackgroundView.setFillPaintColor(0x802191F5);
        mRevealBackgroundView.setOnStateChangeListener(this);
        if (savedInstanceState == null) {
            final int[] startingLocation = getIntent().getIntArrayExtra(ARG_REVEAL_START_LOCATION);
            mRevealBackgroundView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    mRevealBackgroundView.getViewTreeObserver().removeOnPreDrawListener(this);
                    mRevealBackgroundView.startFromLocation(startingLocation);
                    return true;
                }
            });
        } else {
            mRevealBackgroundView.setToFinishedFrame();
        }
    }

    @Override
    public void onStateChange(int state) {
        if (RevealBackgroundView.STATE_FINISHED == state) {
            mRevealBackgroundView.setFillPaintColor(0xffffffff);
            mFrameLayout.setVisibility(View.VISIBLE);
           /* if (pendingIntro) {
                startIntroAnimation();
            }*/
        } else {
            mFrameLayout.setVisibility(View.INVISIBLE);
        }
    }
}
